#!/usr/bin/python
# coding: utf-8

import urllib2
import json

Citycode = {            
            '北京':'101010100',
            "天津":'101030100',
            "石家庄":'101090101',
            "太原":'101100101',
            "呼和浩特":'101080101',
            "保定":'101090201',
            "大同":'101100201',
            "包头":'101080201',
            "承德市":'101090402',
            "晋中":'101100401',
            "秦皇岛":'101091101',
            #东北
            "哈尔滨":'101050101',
            "长春":'101060101',
            "沈阳":'101070101',
            "齐齐哈尔":'101050201',
            "吉林":'101060201',
            "大连":'101070201',
            "牡丹江":'101050301',
            "延吉":'101060301',
            "鞍山":'101070301',
            "绥化":'101050501',
            "白城":'101060601',
            "葫芦岛":'101071401',
            "哈尔滨":'101050101',
            #华南
            "广州":'101280101',
            "南宁":'101300101',
            "海口":'101310101',
            "香港":'101320101',
            "澳门":'101330101',
            "深圳":'101280601',
            "桂林":'101300501',
            "三亚":'101310201',
            "珠海":'101280701',
            "中山":'101281701',
            "百色":'101301001',
            "万宁":'101310215',
            #西北
            "西安":'101110101',
            "兰州":'101160101',
            "西宁":'101150101',
            "银川":'101170101',
            "乌鲁木齐":'101130101',
            "延安":'101110300',
            "宝鸡":'101110901',
            "天水":'101160901',
            "吴忠":'101170301',
            "吐鲁番":'101130501',
            "酒泉":'101160801',
            "固原":'101170401',
            #西南
            "重庆":'101040100',
            "成都":'101270101',
            "贵阳":'101260101',
            "昆明":'101290101',
            "拉萨":'101140101',
            "绵阳":'101270401',
            "遵义":'101260201',
            "大理":'101290201',
            "乐山":'101271401',
            "六盘水":'101260801',
            "丽江":'101291401',
            #华东
            "上海":'101020100',
            "福州":'101230101',
            "合肥":'101220101',
            "南昌":'101240101',
            "济南":'101120101',
            "嘉兴":'101210301',
            "南京":'101190101',
            "宁波":'101210401',
            "杭州":'101210101',
            "苏州":'101190401',
            "青岛":'101120201',
            "厦门":'101230201',
            "台北市":'101340101',
            "南通":'101190501',
            "扬州":'101190601',
            "宿迁":'101191301',
            "徐州":'101190801',
            "淮安":'101190901',
            "连云港":'101191001',
            "常州":'101191101',
            "泰州":'101191201',
            "无锡":'101190201',
            "盐城":'101190701',
            "镇江":'101190301',
            #华中
            "郑州":'101180101',
            "武汉":'101200101',
            "长沙":'101250101',
            "安阳":'101180201',
            "襄阳":'101200201',
            "湘潭":'101250201',
            "株洲":'101250301',
            "许昌":'101180401',
            "常德":'101250601',
            "张家界":'101251101',
            "孝感":'101200401',
            "荆门":'101201401',
            }

def getweather(city):
    if city not in Citycode.keys():
        return u'sorry,该城市查询不到,你可以尝试输入您城市所在的更大一点的城市\n'
    citycode = Citycode[city]
    url = 'http://m.weather.com.cn/data/' + citycode + '.html'
    weatherHtml = urllib2.urlopen(url).read()
    weatherJSON = json.JSONDecoder().decode(weatherHtml)
    weatherInfo = weatherJSON['weatherinfo']
    
    weather_24 = u'城市：%s:\n时间：%s\n------------------------\n24小时天气：\n温度：%s\n天气：%s\n风速：%s\n紫外线：%s\n穿衣指数：%s\n' % (''.join(weatherInfo['city']),''.join(weatherInfo['date_y']),' '.join(weatherInfo['temp1']),' '.join(weatherInfo['weather1']),' '.join(weatherInfo['wind1']),''.join(weatherInfo['index_uv']),''.join(weatherInfo['index_d']))
    weather_48 = u'------------------------\n48小时天气：\n温度：%s\n天气：%s\n风速：%s\n紫外线：%s\n穿衣指数：%s\n' % (' '.join(weatherInfo['temp2']),' '.join(weatherInfo['weather2']),' '.join(weatherInfo['wind2']),''.join(weatherInfo['index48_uv']),''.join(weatherInfo['index48_d']))
    return weather_24 + weather_48
